package com.course.domain.service.impl;

import com.course.domain.dao.SpecInfoDao;
import com.course.domain.dao.SpecWeekDao;
import com.course.domain.model.SpecInfo;
import com.course.domain.model.SpecWeek;
import com.course.domain.service.SpecService;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Justin on 2017/5/12.
 */
@Service
public class SpecServiceImpl implements SpecService {

    private final SpecInfoDao specInfoDao;
    private final SpecWeekDao specWeekDao;

    @Autowired
    public SpecServiceImpl(SpecInfoDao specInfoDao,
                           SpecWeekDao specWeekDao) {
        this.specInfoDao = specInfoDao;
        this.specWeekDao = specWeekDao;
    }

    @Override
    public SpecInfo createInfo(SpecInfo info) {
        Preconditions.checkNotNull(info);
        info.setId(0);
        info.setParts(0);
        info.setState(0);
        info.setStatus(1);
        return specInfoDao.save(info);
    }

    @Override
    public SpecInfo updateInfo(SpecInfo info) {
        Preconditions.checkNotNull(info);
        return specInfoDao.save(info);
    }

    @Override
    public SpecInfo deleteInfo(SpecInfo info) {
        info.setStatus(0);
        return specInfoDao.save(info);
    }

    @Override
    public List<SpecWeek> createWeek(List<SpecWeek> weeks) {
        return specWeekDao.save(weeks);
    }

    @Override
    public List<SpecWeek> updateWeek(List<SpecWeek> weeks) {
        return specWeekDao.save(weeks);
    }

    @Override
    public List<SpecWeek> deleteWeek(List<SpecWeek> weeks) {
        specWeekDao.delete(weeks);
        return weeks;
    }

    @Override
    public SpecInfo loadInfo(int specId) {
        return specInfoDao.findOne(specId);
    }

    @Override
    public List<SpecInfo> listInfo() {
        return specInfoDao.findAllByStatus(1);
    }

    @Override
    public List<SpecInfo> listInfoPublished() {
        return specInfoDao.findAllByStateAndStatus(1, 1);
    }

    @Override
    public List<SpecWeek> findWeek(int specId) {
        return specWeekDao.findBySpecIdOrderByPartIdx(specId);
    }
}
